package com.hjx.movie.core.system.runner;

import com.hjx.movie.common.utils.exceptions.CustomException;
import com.hjx.movie.persistence.system.mapper.IvySysStaticDataMapper;
import com.hjx.movie.persistence.system.model.IvySysStaticData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/1/29 - 21:26
 */
@Slf4j
@Component
@Order(value = 2)
public class SystemStaticData implements ApplicationRunner {
    @Resource
    private IvySysStaticDataMapper staticDataMapper;
    private List<IvySysStaticData> staticDataList = new ArrayList<>();
    private final Map<String,IvySysStaticData> staticDataMap = new HashMap<>();

    @Override
    public void run(ApplicationArguments args) throws Exception {
        initStaticData();
    }

    private void initStaticData(){
        staticDataList = staticDataMapper.selectList(null);
        for (IvySysStaticData data:staticDataList){
            staticDataMap.put(data.getParamKey(),data);
        }
    }
    //刷新参数配置项
    public void refresh(){
        initStaticData();
    }

    public String getStaticValue(String key){
       Optional<IvySysStaticData> temp = Optional.ofNullable(staticDataMap.get(key));
        if (temp.isPresent()){
            return temp.get().getParamValue();
        }else {
            throw new IllegalArgumentException("全局参数"+key+"获取不到对应的值");
        }
    }

    public Map<String,IvySysStaticData> getStaticDataMap(){
        return staticDataMap;
    }
    public List<IvySysStaticData> getStaticDataList(){
        return staticDataList;
    }
}
